package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IJDHeaderFragmentModel;
import com.wgts.wgts.mvp.presenter.IJDHeaderFragmentPersenter;
import com.wgts.wgts.mvp.view.IJDHeaderFragmentView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.TransformUtils;
import com.wgts.wgts.netmodle.req.ProductEvaluateRequest;
import com.wgts.wgts.netmodle.req.UpDownProductRequest;
import com.wgts.wgts.netmodle.resp.ProductEvaluateEntity;
import com.wgts.wgts.rxjava.MVPResultSubscriber;
import com.wgts.wgts.tools.CountDownTime;
import com.wgts.wgts.tools.CountDownTimerListener;
import com.wgts.wgts.tools.Tools;

import java.sql.Time;

import rx.Observable;
import rx.Subscriber;

public class JDHeaderFragmentPersenter implements IJDHeaderFragmentPersenter {

    private IJDHeaderFragmentModel model;

    private IJDHeaderFragmentView view;

    private CountDownTime countDownTime;

    private CountDownTimerListener countDownTimerListener;

    private long countTime;


    public JDHeaderFragmentPersenter(IJDHeaderFragmentModel model, IJDHeaderFragmentView view, CountDownTime countDownTime) {
        this.model = model;
        this.view = view;
        this.countDownTime = countDownTime;
    }

    @Override
    public void productEvaluate(String id, int pageIndex, int pageSize) {
        ProductEvaluateRequest request = new ProductEvaluateRequest();
        request.productId = id;
        request.pageIndex = String.valueOf(pageIndex);
        request.pageSize = String.valueOf(pageSize);
        model.productEvaluate(request, new HttpResultSubscriber<ProductEvaluateEntity>() {
            @Override
            public void onSuccess(ProductEvaluateEntity evaluateEntity) {
                view.evaluateResult(evaluateEntity);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void upDownProduct(String id, int upDownState) {
        UpDownProductRequest request = new UpDownProductRequest();
        request.productId = Integer.valueOf(id);
        request.updown = upDownState;
        model.updownProduct(request, new HttpResultSubscriber<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                view.upDownProductResult(integer);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void startTimeCount(long time) {
        countTime = time;
        if(countDownTimerListener == null){
            countDownTimerListener = new CountDownTimerListener() {
                @Override
                public void countDownTimerListener() {
                    if(countTime > 0){
                        view.timeAction(Tools.change(countTime));
                        countTime--;
                    }else{
                        removeTimeCount();
                        view.timeOverRefresh();
                    }
                }
            };
            countDownTime.addCountDownTimerListener(countDownTimerListener);
        }
    }

    @Override
    public void removeTimeCount() {
        if(countDownTimerListener != null){
            countDownTime.removeCountDownTimerListener(countDownTimerListener);
            countDownTimerListener = null;
        }
    }
}
